<template>
  <div class="wrapper">
    <img
      src="http://www.dell-lee.com/imgs/vue3/user.png"
      alt=""
      class="wrapper__img"
    />
    <div class="wrapper__register wrapper__register1">
      <input
        type="text"
        class="wrapper__register__phone"
        placeholder="请输入手机号"
        v-model="username"
      />
    </div>
    <div class="wrapper__register wrapper__register2">
      <input
        type="password"
        class="wrapper__register__pass"
        placeholder="请输入密码"
        v-model="password"
      />
    </div>
    <div class="wrapper__register wrapper__register3">
      <input
        type="password"
        class="wrapper__register__pass"
        placeholder="确认密码"
        v-model="checkpassword"
      />
    </div>
    <button class="wrapper__btn" @click="handleClick">注册</button>
    <p class="wrapper__link" @click="handleRegisterClick">已有账号去登陆</p>
    <Toast v-if="show" :message="message" />
  </div>
</template>

<script>
import { useRouter } from "vue-router";
import { reactive, toRefs } from "vue";
import { post } from "../../utils/post";
import Toast, { useToast } from "../../components/Toast";

const useHandleClick = (changeMessage) => {
  const router = useRouter();
  const data = reactive({ username: "", password: "", checkpassword: "" });
  const { username, password, checkpassword } = toRefs(data);
  // 设置点击“注册”按钮事件
  const handleClick = async () => {
    console.log(username.value);
    if (
      username.value === "" ||
      password.value === "" ||
      checkpassword.value === ""
    ) {
      changeMessage("用户名或密码不能为空");
      return;
    } else if (password.value !== checkpassword.value) {
      changeMessage("确认密码不一致");
      return;
    }
    try {
      // 给虚拟后台接口发送用户名及密码，确认密码信息
      const result = await post("/api/user/register", {
        username: username.value,
        password: password.value,
        checkpassword: checkpassword.value,
      });
      console.log(result);
      if (result?.errno === 0) {
        // 如果成功接收到信息则跳转到“登陆”页面
        router.push({ name: "Login" });
      } else {
        // 否则弹出“注册失败”窗口
        changeMessage("注册失败");
      }
    } catch (e) {
      changeMessage("请求失败");
    }
  };
  return { username, password, checkpassword, handleClick };
};

const useHandleRegisterClick = () => {
  const router = useRouter();
  const handleRegisterClick = () => {
    router.push({ name: "Login" });
  };
  return { handleRegisterClick };
};

export default {
  name: "Register",
  components: { Toast },
  setup() {
    const { show, message, changeMessage } = useToast();
    const { handleRegisterClick } = useHandleRegisterClick();
    const { username, password, checkpassword, handleClick } = useHandleClick(
      changeMessage
    );
    return {
      username,
      password,
      checkpassword,
      show,
      message,
      handleRegisterClick,
      handleClick,
    };
  },
};
</script>

<style lang="scss" scoped>
@import "../../style/viriables";
.wrapper {
  position: absolute;
  top: 50%;
  left: 0%;
  right: 0%;
  transform: translateY(-50%);
  text-align: center;
  p {
    margin: 0;
  }
  &__img {
    display: block;
    width: 0.66rem;
    height: 0.66rem;
    margin: 0 auto 0.4rem;
  }
  &__register {
    background-color: #f9f9f9;
    height: 0.48rem;
    border-radius: 0.06rem;
    padding-left: 0.16rem;
    input {
      width: 100%;
      height: .22rem;
      margin-top:.13rem;
      border: none;
      outline: none;
      background: none;
      font-size: 0.16rem;
      color: $content-inputcolor;
      padding: 0;
      &::placeholder {
        color: $content-inputcolor;
      }
    }
  }
  &__register1,
  &__register2 {
    margin: 0 0.4rem 0.16rem;
  }
  &__register3 {
    margin: 0 0.4rem 0.32rem;
  }
  &__btn {
    width: 2.95rem;
    height: 0.48rem;
    background-color: $btn-bgcolor;
    color: #fff;
    font-size: 0.16rem;
    border: none;
    outline: none;
    border-radius: 0.04rem;
    margin-bottom: 0.16rem;
  }
  &__link {
    font-size: 0.14rem;
    color: $content-inputcolor;
  }
}
</style>